package com.gnizr.feed ;

import com.gnizr.web.action.AbstractAction ;

import java.util.Date ;
import java.util.List ;
import java.util.Calendar ;

import java.text.SimpleDateFormat ;

import java.sql.Timestamp ;

import org.apache.log4j.Logger ;

import com.gnizr.db.dao.DaoResult ;

public class FeedAction extends AbstractAction
{
    private static final long serialVersionUID = 4604179423988890376L;
   
    private static final Logger logger = Logger.getLogger(FeedAction.class.getName());

    private FeedDao feedDao ;
    
    public FeedAction(FeedDao feedDao) {
        this.feedDao = feedDao ;
    }

    public String getToday() {
        return fetchAllFeeds(todayStart(), todayStop()) ;
    }

    public String getYesterday() {
        return fetchAllFeeds(yesterdayStart(), yesterdayStop()) ;
    }
    
    public String getEarlier() {
        return fetchAllFeeds(earlierStart(), earlierStop()) ;
    }
    
    public String getRss() {
        return fetchAllFeedsRSS(earlierStart(), todayStop()) ;
    }
    
    public String getSpecial() {        
        SimpleDateFormat fmt =
            new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz") ;
        return fmt.format(new Date()) ;
    }
        
    protected String fetchAllFeeds(Timestamp startTime, Timestamp stopTime)
    {
        DaoResult<FeedItem> result ;
        List<FeedItem> feed ;
        String message = "" ;
        
        result = feedDao.fetchAllFeeds(startTime, stopTime) ;
        feed = result.getResult() ;

        message = message + "<ul class=thefeed>" ;
        for ( FeedItem item : feed )
            message = message + item.toString() + "\n" ;
        message = message + "</ul>" ;

        return message ;
    }

    protected String fetchAllFeedsRSS(Timestamp startTime, Timestamp stopTime)
    {
        DaoResult<FeedItem> result ;
        List<FeedItem> feed ;
        String message = "" ;
        
        result = feedDao.fetchAllFeeds(startTime, stopTime) ;
        feed = result.getResult() ;

        for ( FeedItem item : feed )
            message = message + item.toRSS() ;

        return message ;
    }
    
    protected static Timestamp todayStart()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.set(Calendar.HOUR_OF_DAY, 00) ;
        cal.set(Calendar.MINUTE, 00) ;
        cal.set(Calendar.SECOND, 00) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp todayStop()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.set(Calendar.HOUR_OF_DAY, 23) ;
        cal.set(Calendar.MINUTE, 59) ;
        cal.set(Calendar.SECOND, 59) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp yesterdayStart()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.add(Calendar.DAY_OF_MONTH, -1) ;
        cal.set(Calendar.HOUR_OF_DAY, 00) ;
        cal.set(Calendar.MINUTE, 00) ;
        cal.set(Calendar.SECOND, 00) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp yesterdayStop()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.add(Calendar.DAY_OF_MONTH, -1) ;
        cal.set(Calendar.HOUR_OF_DAY, 23) ;
        cal.set(Calendar.MINUTE, 59) ;
        cal.set(Calendar.SECOND, 59) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp earlierStart()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.add(Calendar.DAY_OF_MONTH, -7) ;
        cal.set(Calendar.HOUR_OF_DAY, 00) ;
        cal.set(Calendar.MINUTE, 00) ;
        cal.set(Calendar.SECOND, 00) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp earlierStop()
    {
        Calendar cal = Calendar.getInstance() ;
        cal.add(Calendar.DAY_OF_MONTH, -2) ;
        cal.set(Calendar.HOUR_OF_DAY, 23) ;
        cal.set(Calendar.MINUTE, 59) ;
        cal.set(Calendar.SECOND, 59) ;
        return new Timestamp(cal.getTimeInMillis()) ;
    }
    
    protected static Timestamp createTimestamp(String txt)
    {
        Timestamp ts = null ;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss") ;
            Date parsedDate = dateFormat.parse(txt) ;
            ts = new Timestamp(parsedDate.getTime()) ;
        }
        catch (Exception e) {
            logger.fatal(e) ;
        }
        return ts ;
    }
    
    @Override
    protected String go() throws Exception {
        return SUCCESS ;
    }
}
